package com.chulianyuang.controller;

import com.chulianyuang.mapper.EmployeeMapper;
import com.chulianyuang.pojo.CheckResult;
import com.chulianyuang.pojo.ClaimVoucher;
import com.chulianyuang.pojo.Employee;
import com.chulianyuang.service.CheckResultService;
import com.chulianyuang.service.ClaimVoucherService;
import com.chulianyuang.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;

@Controller
public class CheckResultController {
    @Autowired
    private ClaimVoucherService claimVoucherService;
    @Autowired
    private CheckResultService checkResultService;
    @Autowired
    EmployeeService employeeService;
    @RequestMapping("toCheckVoucher")
    public String toCheckResult(int voucherid, Model model){
        ClaimVoucher voucher=claimVoucherService.selectClaimVoucherById(voucherid);

        model.addAttribute("voucher",voucher);
        return "jsp/claim/claim_voucher_check";
    }
    @RequestMapping("checkResult")
    public String checkResult(@ModelAttribute CheckResult checkResult, HttpSession session,double totalCount){
        Employee emp=(Employee)session.getAttribute("emp");
        Employee employee=new Employee();
        checkResult.setChecker_sn(emp.getSn());
        checkResultService.insertCheckResult(checkResult);
        //创建新对象作为参数丢到更新方法里
       ClaimVoucher claimVoucher=new ClaimVoucher();
       claimVoucher.setId(checkResult.getClaim_id());
//        根据提交状态修改报销单的下一个审批人以及报销单状态
        if(checkResult.getResult().equals("打回")){
            claimVoucher.setStatus("已打回");
            claimVoucher.setNext_deal_sn(null);
        } else if (checkResult.getResult().equals("拒绝")) {
            claimVoucher.setStatus("已拒绝");
            claimVoucher.setNext_deal_sn(null);
        } else if (checkResult.getResult().equals("通过")) {
//            编写审批通过流程

//            判断当前身份
            String posi=emp.getPosition().getName_cn();
            if (posi.equals("部门经理")){
                claimVoucher.setStatus("已审批");
//                判断是否需要提交总经理
                if (totalCount>5000){
                    employee=employeeService.findEmpByPname("总经理");
                    claimVoucher.setNext_deal_sn(employee.getSn());
                }else {
                    //提交财务
                    employee=employeeService.findEmpByPname("财务");
                    claimVoucher.setNext_deal_sn(employee.getSn());

                }
            }else if (posi.equals("总经理")){
                claimVoucher.setStatus("已审批");
                //提交财务
                employee=employeeService.findEmpByPname("财务");
                claimVoucher.setNext_deal_sn(employee.getSn());
            }else if (posi.equals("财务")){
                claimVoucher.setStatus("已付款");
                //流程终止
                claimVoucher.setNext_deal_sn(null);
            }


        }
       claimVoucherService.updateClaimVoucherById(claimVoucher);
        return "forward:findVoucher";
    }
}
